package com.dycx.cms.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dycx.cms.converter.AdConverter;
import com.dycx.cms.converter.LinkConverter;
import com.dycx.cms.entity.AdDO;
import com.dycx.cms.entity.AdTypeDO;
import com.dycx.cms.mapper.LinkMapper;
import com.dycx.cms.dto.LinkDTO;
import com.dycx.cms.entity.LinkDO;
import com.dycx.cms.service.LinkService;
import com.dycx.cms.vo.AdVO;
import com.dycx.cms.vo.LinkVO;
import com.dycx.system.service.OssFileService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@AllArgsConstructor
public class LinkImpl extends ServiceImpl<LinkMapper, LinkDO> implements LinkService {

    private LinkMapper linkMapper;
    private OssFileService ossFileService;

    @Override
    public Boolean delete(Integer id) {
        LinkDO oldDO = this.getById(id);
        if (!this.removeById(id)) return false;

        ossFileService.setDeleted(oldDO.getThumbnail());
        return true;
    }

    /**
     * 添加
     *
     * @param linkDto
     * @return
     */
    @Override
    public Boolean add(LinkDTO linkDto) {
        LinkDO linkDO = LinkConverter.dtoToEntity(linkDto);
        if (!this.save(linkDO)) return false;

        ossFileService.setUse(linkDto.getThumbnail());
        linkDto.setId(linkDO.getId());
        return true;
    }

    /**
     * 修改
     *
     * @param linkDto
     * @return
     */
    @Override
    public Boolean edit(LinkDTO linkDto) {
        LinkDO oldDO = this.getById(linkDto.getId());
        LinkDO linkDO = LinkConverter.dtoToEntity(linkDto);
        if (!this.updateById(linkDO)) return false;

        ossFileService.setDeleted(oldDO.getThumbnail());
        ossFileService.setUse(linkDto.getThumbnail());

        return true;
    }

    /**
     * 分页
     *
     * @return
     */
    @Override
    public Page<LinkVO> getPage(LinkDTO dto) {
        LambdaQueryWrapper<LinkDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(dto.getTitle()), LinkDO::getTitle, dto.getTitle());

        if (StrUtil.isBlank(dto.getOrderBy())) {
            wrapper.orderByDesc(LinkDO::getId);
        } else {
            wrapper.last(" order by " + dto.getOrderBy());
        }

        Page<LinkDO> entityPage = page(new Page<>(dto.getPageIndex(), dto.getPageSize()), wrapper);
        return LinkConverter.entityToVoPage(entityPage);
    }


    /**
     * 查询详细
     *
     * @param id
     * @return
     */
    @Override
    public LinkVO getDetailById(Integer id, Boolean status) {
        LambdaQueryWrapper<LinkDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(LinkDO::getId, id);
        wrapper.eq(ObjectUtil.isNotEmpty(status), LinkDO::getStatus, status);

        return LinkConverter.entityToVo(this.getOne(wrapper));
    }

    /**
     * 更新排序
     *
     * @param linkDTO
     * @return
     */
    @Override
    public Boolean updateSort(LinkDTO linkDTO) {
        LinkDO link = new LinkDO();
        link.setId(linkDTO.getId());
        link.setSort(linkDTO.getSort());
        return this.updateById(link);
    }

    /**
     * 更新状态
     *
     * @param linkDTO
     * @return
     */
    @Override
    public Boolean updateStatus(LinkDTO linkDTO) {
        LinkDO linkDO = new LinkDO();
        linkDO.setId(linkDTO.getId());
        linkDO.setStatus(linkDTO.getStatus());
        return this.updateById(linkDO);
    }

}
